﻿using SRB.Tg.DrawingStandard;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class SteelBarS
    {
        public static void CreatePartSteelBar(Database db, ref Extents2d ext, out Point2d ptEnd, Point2d pt, double radius,
double height, double interval, bool isHorizontal = false, eLocation location = eLocation.Top,bool isTop=true)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (isHorizontal)
                {
                    #region 螺旋钢筋
                    Polyline Line = new Polyline() { Closed = false, LayerId = lt["H钢筋"] };
                    int num = (int)(height / interval);
                    // 螺旋钢筋的拉伸间隔             
                    List<Point2d> list = new List<Point2d>();
                    list.Add(pt);
                    switch (location)
                    {
                        case eLocation.Bottom:
                            Line.AddVertexAt(0, pt.C2D(0, -radius * 0.5), 0, 0, 0);
                            Line.AddVertexAt(0, pt.C2D(0, radius * 0.5), 0, 0, 0);
                            list.Add(pt.C2D(0, -radius * 0.5));
                            list.Add(pt.C2D(0, radius * 0.5));
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = list[list.Count - 1].C2D(-interval * 0.5, -radius);
                                list.Add(pt1.C2D());

                                Point2d pt2 = list[list.Count - 1].C2D(-interval * 0.5, radius);
                                list.Add(pt2.C2D());

                                Line.AddVertexAt(0, pt1, 0, 0, 0);
                                Line.AddVertexAt(0, pt2, 0, 0, 0);
                            }
                            break;
                        case eLocation.Top:
                            Line.AddVertexAt(0, pt, 0, 0, 0);
                            Line.AddVertexAt(0, pt.C2D(-interval * 0.5, -radius), 0, 0, 0);
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = list[list.Count - 1].C2D(-interval * 0.5, -radius);
                                list.Add(pt1.C2D());

                                Point2d pt2 = list[list.Count - 1].C2D(-interval * 0.5, radius);
                                list.Add(pt2.C2D());

                                Line.AddVertexAt(0, pt1, 0, 0, 0);
                                Line.AddVertexAt(0, pt2, 0, 0, 0);
                            }
                            if (isTop)
                            {
                                Line.AddVertexAt(0, pt.C2D(-height, 0), 0, 0, 0);
                                Line.AddVertexAt(0, pt.C2D(-height, -radius), 0, 0, 0);
                                list.Add(pt.C2D(-height, radius));
                                list.Add(pt.C2D(-height, -radius));
                            }
                            break;
                        case eLocation.Middle:
                            Line.AddVertexAt(0, pt, 0, 0, 0);
                            Line.AddVertexAt(0, pt.C2D(-interval * 0.5, -radius), 0, 0, 0);
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = list[list.Count - 1].C2D(-interval * 0.5, -radius);
                                list.Add(pt1.C2D());

                                Point2d pt2 = list[list.Count - 1].C2D(-interval * 0.5, radius);
                                list.Add(pt2.C2D());

                                Line.AddVertexAt(0, pt1, 0, 0, 0);
                                Line.AddVertexAt(0, pt2, 0, 0, 0);
                            }
                            break;
                    }

                    ptEnd = list[list.Count - 1];
                    ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                    res.Add(Line);
                    #endregion

                    foreach (DBObject drawitem in res)
                    {
                        recorder.AppendEntity((Entity)drawitem);
                        tr.AddNewlyCreatedDBObject(drawitem, true);
                    }
                }
                else
                {
                    #region 螺旋钢筋
                    // 螺旋钢筋的拉伸间隔             
                    List<Point2d> list = new List<Point2d>();
                    Polyline Line = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                    int num = (int)(height / interval);
                    list.Add(pt);
                    switch (location)
                    {
                        case eLocation.Bottom:
                            if (isTop)
                            {
                                Line.AddVertexAt(0, pt.C2D(-radius * 0.5), 0, 0, 0);
                                Line.AddVertexAt(0, pt.C2D(radius * 0.5), 0, 0, 0);

                                list.Add(pt.C2D(-radius * 0.5));
                                list.Add(pt.C2D(radius * 0.5));
                            }
                            else
                            {
                                Line.AddVertexAt(0, pt.C2D(), 0, 0, 0);
                                list.Add(pt.C2D());
                            }
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = list[list.Count - 1].C2D(-radius, interval * 0.5);
                                list.Add(pt1.C2D());
                                Point2d pt2 = list[list.Count - 1].C2D(radius, interval * 0.5);
                                list.Add(pt2.C2D());
                                Line.AddVertexAt(0, pt1, 0, 0, 0);
                                Line.AddVertexAt(0, pt2, 0, 0, 0);
                            }
                            break;
                        case eLocation.Top:
                            Line.AddVertexAt(0, pt, 0, 0, 0);
                            Line.AddVertexAt(0, pt.C2D(-radius, interval * 0.5), 0, 0, 0);
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = list[list.Count - 1].C2D(-radius, interval * 0.5);
                                list.Add(pt1.C2D());
                                Point2d pt2 = list[list.Count - 1].C2D(radius, interval * 0.5);
                                list.Add(pt2.C2D());
                                Line.AddVertexAt(0, pt1, 0, 0, 0);
                                Line.AddVertexAt(0, pt2, 0, 0, 0);
                            }
                            if (isTop)
                            {
                                Line.AddVertexAt(0, pt.C2D(0, height), 0, 0, 0);
                                Line.AddVertexAt(0, pt.C2D(-radius, height), 0, 0, 0);

                                list.Add(pt.C2D(0, height));
                            }
                            //list.Add(pt.C2D(-radius, height));
                            break;
                        case eLocation.Middle:
                            Line.AddVertexAt(0, pt, 0, 0, 0);
                            Line.AddVertexAt(0, pt.C2D(-radius, interval * 0.5), 0, 0, 0);
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = list[list.Count - 1].C2D(-radius, interval * 0.5);
                                list.Add(pt1.C2D());
                                Point2d pt2 = list[list.Count - 1].C2D(radius, interval * 0.5);
                                list.Add(pt2.C2D());
                                Line.AddVertexAt(0, pt1, 0, 0, 0);
                                Line.AddVertexAt(0, pt2, 0, 0, 0);
                            }
                            break;
                    }
                    ptEnd = list[list.Count - 1];
                    ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                    res.Add(Line);
                    #endregion

                    foreach (DBObject drawitem in res)
                    {
                        recorder.AppendEntity((Entity)drawitem);
                        tr.AddNewlyCreatedDBObject(drawitem, true);
                    }
                }
                tr.Commit();

            }
        }

        public static void CreatePierPartSteelBar(Database db, ref Extents2d ext, out Point2d ptEnd, Point2d pt, double radius, double radiusP,
double height, double interval, bool isHorizontal = false, eLocation location = eLocation.Top)
        {
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (isHorizontal)
                {
                    #region 螺旋钢筋
                    Polyline Line = new Polyline() { Closed = false, LayerId = lt["H钢筋"] };
                    int num = (int)(height / interval);
                    // 螺旋钢筋的拉伸间隔             
                    List<Point2d> list = new List<Point2d>();
                    list.Add(pt);
                    switch (location)
                    {
                        case eLocation.Bottom:
                            Line.AddVertexAt(0, pt.C2D(0, -radius), 0, 0, 0);
                            Line.AddVertexAt(0, pt.C2D(0), 0, 0, 0);
                            list.Add(pt.C2D(0, -radius));
                            list.Add(pt.C2D(0, radius));
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = MathExtensions.GetPointOnTheLine(pt.C2D(0, -radius), pt.C2D(-height, -radius * 0.5 - radiusP * 0.5), interval * 0.5 + i * interval);
                                Point2d pt2 = MathExtensions.GetPointOnTheLine(pt, pt.C2D(-height, -radius * 0.5 + radiusP * 0.5), interval + i * interval);
                                list.Add(new Point2d(pt.X - interval * 0.5 - i * interval, pt1.Y));
                                list.Add(new Point2d(pt.X - interval - i * interval, pt2.Y));

                                Line.AddVertexAt(0, new Point2d(pt.X - interval * 0.5 - i * interval, pt1.Y), 0, 0, 0);
                                Line.AddVertexAt(0, new Point2d(pt.X - interval - i * interval, pt2.Y), 0, 0, 0);
                            }
                            break;
                        case eLocation.Top:
                            Line.AddVertexAt(0, pt, 0, 0, 0);
                            //Line.AddVertexAt(0, pt.C2D(-interval * 0.5, -radius), 0, 0, 0);
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = MathExtensions.GetPointOnTheLine(pt.C2D(0, -radius), pt.C2D(-height, -radius * 0.5 - radiusP * 0.5), interval * 0.5 + i * interval);
                                Point2d pt2 = MathExtensions.GetPointOnTheLine(pt, pt.C2D(-height, -radius * 0.5 + radiusP * 0.5), interval + i * interval);
                                list.Add(new Point2d(pt.X-interval * 0.5 - i * interval, pt1.Y));
                                list.Add(new Point2d(pt.X - interval - i * interval, pt2.Y));

                                Line.AddVertexAt(0, new Point2d(pt.X - interval * 0.5 - i * interval, pt1.Y), 0, 0, 0);
                                Line.AddVertexAt(0, new Point2d(pt.X - interval - i * interval, pt2.Y), 0, 0, 0);
                            }
                         
                              
                                //Line.AddVertexAt(0, pt.C2D(-height, -radius * 0.5 -radiusP * 0.5), 0, 0, 0);
                                Line.AddVertexAt(0, new Point2d(pt.X - height, pt.Y- radius * 0.5 - radiusP * 0.5), 0, 0, 0);
                            //list.Add(pt.C2D(-height, -radius * 0.5 - radiusP * 0.5));
                                //list.Add(pt.C2D(-height, -radius * 0.5 + radiusP * 0.5));
                            
                            break;
                        case eLocation.Middle:
                            Line.AddVertexAt(0, pt, 0, 0, 0);
                            //Line.AddVertexAt(0, pt.C2D(-interval * 0.5, -radius), 0, 0, 0);
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = MathExtensions.GetPointOnTheLine(pt.C2D(0, -radius), pt.C2D(-height, -radius * 0.5 - radiusP * 0.5), interval * 0.5 + i * interval);
                                Point2d pt2 = MathExtensions.GetPointOnTheLine(pt, pt.C2D(-height, -radius * 0.5 + radiusP * 0.5), interval + i * interval);
                                list.Add(new Point2d(pt.X - interval * 0.5 - i * interval, pt1.Y));
                                list.Add(new Point2d(pt.X - interval - i * interval, pt2.Y));

                                Line.AddVertexAt(0, new Point2d(pt.X - interval * 0.5 - i * interval, pt1.Y), 0, 0, 0);
                                Line.AddVertexAt(0, new Point2d(pt.X - interval - i * interval, pt2.Y), 0, 0, 0);
                            }
                            break;
                    }

                    ptEnd = list[list.Count - 1];
                    ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                    res.Add(Line);
                    #endregion

                    foreach (DBObject drawitem in res)
                    {
                        recorder.AppendEntity((Entity)drawitem);
                        tr.AddNewlyCreatedDBObject(drawitem, true);
                    }
                }
                else
                {
                    #region 螺旋钢筋
                    // 螺旋钢筋的拉伸间隔             
                    List<Point2d> list = new List<Point2d>();
                    Polyline Line = new Polyline() { Closed = false, LayerId = lt["H粗线"] };
                    int num = (int)(height / interval);
                    //list.Add(pt);
                    switch (location)
                    {
                        case eLocation.Bottom:
                            Line.AddVertexAt(0, pt.C2D(-radius*0.5), 0, 0, 0);
                            Line.AddVertexAt(0, pt.C2D(radius * 0.5), 0, 0, 0);
                            list.Add(pt.C2D( -radius * 0.5));
                            list.Add(pt.C2D(radius * 0.5));
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = MathExtensions.GetPointOnTheLine(pt.C2D(-radius*0.5), pt.C2D(- radiusP * 0.5,height), interval * 0.5 + i * interval);
                                Point2d pt2 = MathExtensions.GetPointOnTheLine(pt.C2D(radius * 0.5), pt.C2D( radiusP * 0.5, height), interval + i * interval);
                          
                                Line.AddVertexAt(0, pt1, 0, 0, 0);
                                Line.AddVertexAt(0, pt2, 0, 0, 0);
                                list.Add(pt1);
                                list.Add(pt2);
                            }
                            break;
                        case eLocation.Top:
                            //Line.AddVertexAt(0, pt, 0, 0, 0);
                            //Line.AddVertexAt(0, pt.C2D(-interval * 0.5, -radius), 0, 0, 0);
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = MathExtensions.GetPointOnTheLine(pt.C2D(-radius * 0.5), pt.C2D(-radiusP * 0.5, height), interval * 0.5 + i * interval);
                                Point2d pt2 = MathExtensions.GetPointOnTheLine(pt.C2D(radius * 0.5), pt.C2D(radiusP * 0.5, height), interval + i * interval);
                                list.Add(pt1);
                                list.Add(pt2);
                                Line.AddVertexAt(0, pt1, 0, 0, 0);
                                Line.AddVertexAt(0, pt2, 0, 0, 0);
                            }

                            Line.AddVertexAt(0, pt.C2D(-radiusP * 0.5), 0, 0, 0);
                            Line.AddVertexAt(0, pt.C2D(radiusP * 0.5), 0, 0, 0);
                            list.Add(pt.C2D(-radiusP * 0.5));
                            list.Add(pt.C2D(radiusP * 0.5));
                            //Line.AddVertexAt(0, pt.C2D(-height, -radius * 0.5 -radiusP * 0.5), 0, 0, 0);
                            //Line.AddVertexAt(0, new Point2d(pt.X - height, pt.Y - radius * 0.5 - radiusP * 0.5), 0, 0, 0);
                            //list.Add(pt.C2D(-height, -radius * 0.5 - radiusP * 0.5));
                            //list.Add(pt.C2D(-height, -radius * 0.5 + radiusP * 0.5));

                            break;
                        case eLocation.Middle:
                            //Line.AddVertexAt(0, pt, 0, 0, 0);
                            //Line.AddVertexAt(0, pt.C2D(-interval * 0.5, -radius), 0, 0, 0);
                            for (int i = 0; i < num; ++i)
                            {
                                Point2d pt1 = MathExtensions.GetPointOnTheLine(pt.C2D(-radius * 0.5), pt.C2D(-radiusP * 0.5, height), interval * 0.5 + i * interval);
                                Point2d pt2 = MathExtensions.GetPointOnTheLine(pt.C2D(radius * 0.5), pt.C2D(radiusP * 0.5, height), interval + i * interval);
                                list.Add(pt1);
                                list.Add(pt2);
                                Line.AddVertexAt(0, pt1, 0, 0, 0);
                                Line.AddVertexAt(0, pt2, 0, 0, 0);
                            }
                            break;
                    }

                    ptEnd = list[list.Count - 1];
                    ext = ext.Add(new Extents2d(Line.Bounds.Value.MinPoint.C2D(), Line.Bounds.Value.MaxPoint.C2D()));
                    res.Add(Line);
                    #endregion

                    foreach (DBObject drawitem in res)
                    {
                        recorder.AppendEntity((Entity)drawitem);
                        tr.AddNewlyCreatedDBObject(drawitem, true);
                    }
                }
                tr.Commit();

            }
        }
        public static void CreateRib(Database db, ref Extents2d ext, Point2d pt, bool isUpper = true, double gradeLaye = 7.5, double w0 = 6.6, string layer = "H钢筋", int colorindex = 4, bool isDim = false, double w = 16, double scale = 1)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                if (isUpper)
                {
                    //Arc arcCal = new Arc(pt1.C3D(), pile_diameter * 0.5, GeTools.DegreeToRadian(0), GeTools.DegreeToRadian(360));
                    PLPloter.AddLineWithColor(db,pt.C2D(w * 0.5 - 1.4), pt.C2D(-(w * 0.5 - 1.4)), ref ext, "H钢筋", 4);
                    double ang0 = GeTools.RadianToDegree(Math.Atan(w0 / gradeLaye));
                    Arc arc1 = new Arc(pt.C3D(w * 0.5 - 1.4, -1 * scale), 1 * scale, GeTools.DegreeToRadian(ang0), GeTools.DegreeToRadian(90)) { ColorIndex = 4 };
                    Arc arc2 = new Arc(pt.C3D(-(w * 0.5 - 1.4), -1 * scale), 1 * scale, GeTools.DegreeToRadian(90), GeTools.DegreeToRadian(180 - ang0)) { ColorIndex = 4 };
                    recorder.AppendEntity((Entity)arc1);
                    tr.AddNewlyCreatedDBObject(arc1, true);
                    recorder.AppendEntity((Entity)arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);
                    Arc arc3 = new Arc(pt.C3D(w * 0.5 - 1.4 + w0, 1 * scale - gradeLaye), 1 * scale, GeTools.DegreeToRadian(180 + ang0), GeTools.DegreeToRadian(-90)) { ColorIndex = 4 };
                    Arc arc4 = new Arc(pt.C3D(-(w * 0.5 - 1.4) - w0, 1 * scale - gradeLaye), 1 * scale, GeTools.DegreeToRadian(270), GeTools.DegreeToRadian(-ang0)) { ColorIndex = 4 };
                    recorder.AppendEntity((Entity)arc3);
                    tr.AddNewlyCreatedDBObject(arc3, true);
                    recorder.AppendEntity((Entity)arc4);
                    tr.AddNewlyCreatedDBObject(arc4, true);

                    PLPloter.AddLineWithColor(db,arc1.StartPoint.C2D(), arc3.StartPoint.C2D(), ref ext, "H钢筋", 4);
                    PLPloter.AddLineWithColor(db,arc2.EndPoint.C2D(), arc4.EndPoint.C2D(), ref ext, "H钢筋", 4);

                    PLPloter.AddLineWithColor(db,arc3.EndPoint.C2D(), pt.C2D(w * 0.5 + w0 + w * 0.5, -gradeLaye), ref ext, "H钢筋", 4);
                    PLPloter.AddLineWithColor(db,arc4.StartPoint.C2D(), pt.C2D(-w * 0.5 - w0 - w * 0.5, -gradeLaye), ref ext, "H钢筋", 4);
                }
                else
                {
                    PLPloter.AddLineWithColor(db,pt.C2D(w * 0.5 - 1.4), pt.C2D(-(w * 0.5 - 1.4)), ref ext, "H钢筋", 4);
                    double ang0 = GeTools.RadianToDegree(Math.Atan(w0 / gradeLaye));
                    Arc arc1 = new Arc(pt.C3D(w * 0.5 - 1.4, 1 * scale), 1 * scale, GeTools.DegreeToRadian(270), GeTools.DegreeToRadian(-ang0)) { ColorIndex = 4 };
                    Arc arc2 = new Arc(pt.C3D(-(w * 0.5 - 1.4), 1 * scale), 1 * scale, GeTools.DegreeToRadian(180 + ang0), GeTools.DegreeToRadian(-90)) { ColorIndex = 4 };
                    recorder.AppendEntity((Entity)arc1);
                    tr.AddNewlyCreatedDBObject(arc1, true);
                    recorder.AppendEntity((Entity)arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);
                    Arc arc3 = new Arc(pt.C3D(w * 0.5 - 1.4 + w0, -1 * scale + gradeLaye), 1 * scale, GeTools.DegreeToRadian(90), GeTools.DegreeToRadian(180 - ang0)) { ColorIndex = 4 };
                    Arc arc4 = new Arc(pt.C3D(-(w * 0.5 - 1.4) - w0, -1 * scale + gradeLaye), 1 * scale, GeTools.DegreeToRadian(ang0), GeTools.DegreeToRadian(90)) { ColorIndex = 4 };
                    recorder.AppendEntity((Entity)arc3);
                    tr.AddNewlyCreatedDBObject(arc3, true);
                    recorder.AppendEntity((Entity)arc4);
                    tr.AddNewlyCreatedDBObject(arc4, true);

                    PLPloter.AddLineWithColor(db,arc1.EndPoint.C2D(), arc3.EndPoint.C2D(), ref ext, "H钢筋", 4);
                    PLPloter.AddLineWithColor(db,arc2.StartPoint.C2D(), arc4.StartPoint.C2D(), ref ext, "H钢筋", 4);

                    PLPloter.AddLineWithColor(db,arc3.StartPoint.C2D(), pt.C2D(w * 0.5 + w0 + w * 0.5, gradeLaye), ref ext, "H钢筋", 4);
                    PLPloter.AddLineWithColor(db,arc4.EndPoint.C2D(), pt.C2D(-w * 0.5 - w0 - w * 0.5, gradeLaye), ref ext, "H钢筋", 4);
                }

                tr.Commit();
            }

        }

        public static void CreateRibDBTxt(Database db, ref Extents2d ext, Point2d pt, double dis, bool isUpper = true, double scale = 1)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                if (isUpper)
                {
                    PLPloter.AddLine(db,pt.C2D(-1 * scale, -1 * scale), pt.C2D(1 * scale, 1 * scale), ref ext, "HM细线");
                    PLPloter.AddLine(db,pt.C2D(), pt.C2D(0, 5 * scale), ref ext, "HM细线");
                    Extents2d extT = new Extents2d(pt.C2D(2 * scale, 5 * scale + 2 * scale), pt.C2D(2 * scale, 5 * scale + 2 * scale));
                    TextPloter.AddDBTextWithCirle(db,pt.C3D(2 * scale, 5 * scale + 2 * scale), ref extT, "%%C12/" + dis, "5", scale);
                    PLPloter.AddLine(db,pt.C2D(0, 5 * scale), pt.C2D(extT.W(), 5 * scale), ref ext, "HM细线");
                }
                else
                {
                    PLPloter.AddLine(db,pt.C2D(-1 * scale, 1 * scale), pt.C2D(1 * scale, -1 * scale), ref ext, "HM细线");
                    PLPloter.AddLine(db,pt.C2D(), pt.C2D(0, -5 * scale), ref ext, "HM细线");
                    Extents2d extT = new Extents2d(pt.C2D(2 * scale, -5 * scale - 2 * scale), pt.C2D(2 * scale, -5 * scale - 2 * scale));
                    TextPloter.AddDBTextWithCirle(db,pt.C3D(2 * scale, -5 * scale - 2 * scale), ref extT, "%%C12/" + dis, "5", scale);
                    PLPloter.AddLine(db,pt.C2D(0, -5 * scale), pt.C2D(extT.W(), -5 * scale), ref ext, "HM细线");
                }

                tr.Commit();
            }

        }

        public static void CreateRibRec(Database db, Point2d center, double w, double h, ref Extents2d ext, string layer)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true };
                Point2d ptop, pbot;
                Point2d p11, p12, p13, p14;
                ptop = center.C2D(0, h * 0.5);
                pbot = center.C2D(0, -h * 0.5);
                p11 = ptop.C2D(-w * 0.5);
                p12 = pbot.C2D(-w * 0.5);
                p13 = ptop.C2D(w * 0.5);
                p14 = pbot.C2D(w * 0.5);
                PL1.AddVertexAt(0, p12, 0, 0, 0);
                PL1.AddVertexAt(1, pbot, 0, 0, 0);
                PL1.AddVertexAt(2, p14, 0, 0, 0);

                PL1.AddVertexAt(3, p13, 0, 0, 0);
                PL1.AddVertexAt(4, ptop, 0, 0, 0);
                PL1.AddVertexAt(5, p11, 0, 0, 0);

                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);
                HatchPloter.HatchEnity(db, "SOLID", 1, 0, 4, PL1.ObjectId);
                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();


            }

        }

        public static void CreateRibSection(Database db, Point2d pt, string part, ref Extents2d ext, string layer, double scale = 1, bool isLeft = true)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true };

                Point2d p11, p12;
                Point2d pt0 = pt.C2D();
                if (isLeft)
                {
                    p11 = pt0.C2D(1.5 * scale, 2.5 * scale * 0.5);
                    p12 = pt0.C2D(1.5 * scale, -2.5 * scale * 0.5);
                    PL1.AddVertexAt(0, pt0, 0, 0, 0);
                    PL1.AddVertexAt(1, p11, 0, 0, 0);

                    PL1.AddVertexAt(2, p12, 0, 0, 0);

                    modelSpace.AppendEntity(PL1);
                    tr.AddNewlyCreatedDBObject(PL1, true);
                    PLPloter.AddLine(db,p11.C2D(1 * scale), p12.C2D(1 * scale), ref ext, layer);
                }
                else
                {
                    p11 = pt0.C2D(1.5 * scale, 2.5 * scale * 0.5);
                    p12 = pt0.C2D(1.5 * scale, -2.5 * scale * 0.5);
                    PL1.AddVertexAt(0, pt0.C2D(3*scale), 0, 0, 0);
                    PL1.AddVertexAt(1, p11, 0, 0, 0);

                    PL1.AddVertexAt(2, p12, 0, 0, 0);

                    modelSpace.AppendEntity(PL1);
                    tr.AddNewlyCreatedDBObject(PL1, true);
                    PLPloter.AddLine(db,p11.C2D(-1 * scale), p12.C2D(-1 * scale), ref ext, layer);
                }
                TextPloter.AddDBText(db,pt.C3D(), ref ext, part, scale, 2.5, "SRB.HT", 0, TextHorizontalMode.TextRight, TextVerticalMode.TextVerticalMid);
                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();


            }

        }

        public static void CreateUPRibSection(Database db, Point2d pt, string part, ref Extents2d ext, string layer, double scale = 1,bool isTop=false)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { Closed = true, LayerId = lt[layer], Plinegen = true };

                Point2d p11, p12;
                Point2d pt0 = pt.C2D();
                if (isTop)
                {
                    p11 = pt0.C2D( 2.5 * scale * 0.5, 1.5 * scale);
                    p12 = pt0.C2D( -2.5 * scale * 0.5, 1.5 * scale);
                    PL1.AddVertexAt(0, pt0, 0, 0, 0);
                    PL1.AddVertexAt(1, p11, 0, 0, 0);

                    PL1.AddVertexAt(2, p12, 0, 0, 0);

                    modelSpace.AppendEntity(PL1);
                    tr.AddNewlyCreatedDBObject(PL1, true);
                    PLPloter.AddLine(db,p11.C2D(0, 0.5 * scale), p12.C2D(0, 0.5 * scale), ref ext, layer);
                    TextPloter.AddDBText(db,pt.C3D(0, 3.5 * scale), ref ext, part, scale, 2.5, "SRB.HT",0, TextHorizontalMode.TextMid, TextVerticalMode.TextTop);
                }
                else
                {
                    p11 = pt0.C2D( 2.5 * scale * 0.5, -1.5 * scale);
                    p12 = pt0.C2D( -2.5 * scale * 0.5,-1.5 * scale);
                    PL1.AddVertexAt(0, pt0, 0, 0, 0);
                    PL1.AddVertexAt(1, p11, 0, 0, 0);

                    PL1.AddVertexAt(2, p12, 0, 0, 0);

                    modelSpace.AppendEntity(PL1);
                    tr.AddNewlyCreatedDBObject(PL1, true);
                    PLPloter.AddLine(db,p11.C2D(0,-0.5 * scale), p12.C2D(0,-0.5 * scale), ref ext, layer);
                    TextPloter.AddDBText(db,pt.C3D(0, -3.5 * scale), ref ext, part, scale, 2.5, "SRB.HT", 0, TextHorizontalMode.TextMid, TextVerticalMode.TextBottom);
                }
                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();


            }

        }

        /// <summary>
        /// 增加引线
        /// </summary>
        /// <param name="anchor">标注点</param>
        /// <param name="textAnchor">文字左上角点</param>
        /// <param name="cont">文字内容</param>
        /// <param name="ext"></param>
        /// <param name="scale">比例</param>
        public static void CreateRibMLeader(Point2d pt, Point2d ptM, string cont, string ctxt, ref Extents2d ext, double scale, string layer,double ang=0)
        {
            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary mlstyles = (DBDictionary)tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead);
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;

                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                     OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { LayerId = lt[layer], Plinegen = true };
                Point2d p11 = ptM, p12 = ptM;
                if (ptM.Y > pt.Y)
                {
                    if (ptM.X > pt.X)//左上
                    {
                        Extents2d extT = new Extents2d(ptM.C2D(2 * scale, 2 * scale), ptM.C2D(2 * scale, 2 * scale));
                        TextPloter.AddDBTextWithCirle(db,ptM.C3D(2 * scale, 2 * scale), ref extT, cont, ctxt, scale);
                        p11 = ptM;
                        p12 = ptM.C2D(extT.W() + 3 * scale);
                    }
                    else//右上                  
                    {
                        Extents2d extT = new Extents2d(ptM.C2D(-20 * scale, 2 * scale), ptM.C2D(-20 * scale, 2 * scale));
                        TextPloter.AddDBTextWithCirle(db,ptM.C3D(-18 * scale, 2 * scale), ref extT, cont, ctxt, scale);
                        p11 = ptM;
                        p12 = ptM.C2D(-20 * scale);
                    }
                }
                else
                {
                    if (ptM.X > pt.X)//右下
                    {
                        Extents2d extT = new Extents2d(ptM.C2D(2 * scale, 2 * scale), ptM.C2D(2 * scale, 2 * scale));
                        TextPloter.AddDBTextWithCirle(db,ptM.C3D(2 * scale, 2 * scale), ref extT, cont, ctxt, scale);
                        p11 = ptM;
                        p12 = ptM.C2D(extT.W() + 3 * scale);
                    }
                    else//左下                  
                    {
                        Extents2d extT = new Extents2d(ptM.C2D(-20 * scale, 2 * scale), ptM.C2D(-20 * scale, 2 * scale));
                        TextPloter.AddDBTextWithCirle(db,ptM.C3D(-18 * scale, 2 * scale), ref extT, cont, ctxt, scale);
                        p11 = ptM;
                        p12 = ptM.C2D(-20 * scale);
                    }
                }


                //PLPloter.AddLine(db,pt.C2D(0, 5 * scale), pt.C2D(extT.W(), 5 * scale), ref ext, "HM细线");


                PL1.AddVertexAt(0, pt, 0, 0, 0);
                PL1.AddVertexAt(1, p11, 0, 0, 0);

                PL1.AddVertexAt(2, p12, 0, 0, 0);

                PL1.Rotate(pt.C3D(),ang);
                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);

                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));

                tr.Commit();

            }

        }
        public static void CreateRibRecMLeader(Point2d pt, Point2d ptM, string cont, string contS, ref Extents2d ext, double scale, string layer)
        {
            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary mlstyles = (DBDictionary)tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead);
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;

                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                     OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Polyline PL1 = new Polyline() { LayerId = lt[layer], Plinegen = true };
                Point2d p11 = ptM, p12 = ptM;
                if (ptM.Y > pt.Y)
                {
                    if (ptM.X > pt.X)//左上
                    {
                        Extents2d extT = new Extents2d(ptM.C2D(2 * scale, 0.5 * scale), ptM.C2D(2 * scale, 0.5 * scale));
                        TextPloter.AddDBText(db,ptM.C3D(2 * scale, 0.5 * scale), ref extT, cont, scale);
                   
                        if (!string.IsNullOrWhiteSpace(contS))
                        {
                            TextPloter.AddDBText(db,ptM.C3D(2 * scale, -4 * scale), ref extT, contS, scale);
                        }
                        p11 = ptM;
                        p12 = ptM.C2D(extT.W() + 3 * scale);
                    }
                    else//右上                  
                    {
                        double width=0,height = 0;
                        MText txt1 = TextPloter.GetTextActualWidth(db, cont, scale, out width, out height, 2.5, Extensions.curFont);
                        double width1 = 0;
                        MText txt2 = TextPloter.GetTextActualWidth(db, contS, scale, out width1, out height, 2.5, Extensions.curFont);
                        width = width > width1 ? width : width1;
                        Extents2d extT = new Extents2d(ptM.C2D(-width - 6 * scale, 0.5 * scale), ptM.C2D(-width - 6 * scale, 0.5 * scale));
                        TextPloter.AddDBText(db,ptM.C3D(-width - 5 * scale, 0.5 * scale), ref extT, cont, scale);
                      
                        if (!string.IsNullOrWhiteSpace(contS))
                        {
                            TextPloter.AddDBText(db,ptM.C3D(-width - 5 * scale, -4 * scale), ref extT, contS, scale);
                        }
                        p11 = ptM;
                        p12 = ptM.C2D(-width - 6 * scale);
                    }
                }
                else
                {
                    if (ptM.X > pt.X)//右下
                    {
                        Extents2d extT = new Extents2d(ptM.C2D(2 * scale, 0.5 * scale), ptM.C2D(2 * scale, 0.5 * scale));
                        TextPloter.AddDBText(db,ptM.C3D(2 * scale, 0.5 * scale), ref extT, cont, scale);
                      
                        if (!string.IsNullOrWhiteSpace(contS))
                        {
                            TextPloter.AddDBText(db,ptM.C3D(2 * scale, -4 * scale), ref extT, contS, scale);
                        }
                        p11 = ptM;
                        p12 = ptM.C2D(extT.W() + 3 * scale);
                    }
                    else//左下                  
                    {
                        double width = 0, height = 0;
                        MText txt1 = TextPloter.GetTextActualWidth(db, cont, scale, out width, out height, 2.5, Extensions.curFont);
                        double width1 = 0;
                        MText txt2 = TextPloter.GetTextActualWidth(db, contS, scale, out width1, out height, 2.5, Extensions.curFont);
                        width = width > width1 ? width : width1;
                        Extents2d extT = new Extents2d(ptM.C2D(-width - 6 * scale, 0.5 * scale), ptM.C2D(-width - 6 * scale, 0.5 * scale));
                        TextPloter.AddDBText(db,ptM.C3D(-width - 5* scale, 0.5 * scale), ref extT, cont, scale);
                    
                        if (!string.IsNullOrWhiteSpace(contS))
                        {
                            TextPloter.AddDBText(db,ptM.C3D(-width, -5 * scale), ref extT, contS, scale);
                        }
                        p11 = ptM;
                        p12 = ptM.C2D(-width - 6 * scale);
                    }
                }


                //PLPloter.AddLine(db,pt.C2D(0, 5 * scale), pt.C2D(extT.W(), 5 * scale), ref ext, "HM细线");


                PL1.AddVertexAt(0, pt, 0, 0, 0);
                PL1.AddVertexAt(1, p11, 0, 0, 0);

                PL1.AddVertexAt(2, p12, 0, 0, 0);


                modelSpace.AppendEntity(PL1);
                tr.AddNewlyCreatedDBObject(PL1, true);

                ext = ext.Add(new Extents2d(PL1.Bounds.Value.MinPoint.C2D(), PL1.Bounds.Value.MaxPoint.C2D()));

                tr.Commit();

            }

        }


        public static void CreateMainSample(Database db, ref Extents2d ext, Point2d pt, out Line line1, out Line line2,double wP = 710, double wPi = 298, double wM = 195, bool isUpper = true, double gradeLaye = 28, double w0 = 190, string layer = "H钢筋",  bool isDim = false, double scale = 1,double R=100)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (isUpper)
                {
                    //Arc arcCal = new Arc(pt1.C3D(), pile_diameter * 0.5, GeTools.DegreeToRadian(0), GeTools.DegreeToRadian(360));
                    PLPloter.AddLine(db,pt.C2D(), pt.C2D(wP-5), ref ext, layer);
                    line1 = new Line( pt.C3D(wP - 5), pt.C3D(wP + w0 + 5));
                    double ang0 = GeTools.RadianToDegree(Math.Atan(w0 / gradeLaye));
                    Arc arc1 = new Arc(pt.C3D(wP-5, R), R, GeTools.DegreeToRadian(270), GeTools.DegreeToRadian(-ang0)) { LayerId=lt[layer] };
                    recorder.AppendEntity((Entity)arc1);
                    tr.AddNewlyCreatedDBObject(arc1, true);

                    Arc arc2 = new Arc(pt.C3D(wP+w0+5, -R + gradeLaye), R, GeTools.DegreeToRadian(90), GeTools.DegreeToRadian(180 - ang0)) { LayerId = lt[layer] };
                    recorder.AppendEntity((Entity)arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);



                     PLPloter.AddLine(db,arc2.EndPoint.C2D(), arc1.EndPoint.C2D(), ref ext, layer);
                    line2 = new Line(arc1.EndPoint.C3D(),arc2.EndPoint.C3D());
                    PLPloter.AddLine(db,arc2.StartPoint.C2D(), pt.C2D(wP + w0+wPi, gradeLaye), ref ext, layer);
                }
                else
                {
                    PLPloter.AddLine(db,pt.C2D(), pt.C2D(wP-5), ref ext, layer);
                    line1 = new Line(pt.C3D(wP - 5),pt.C3D(wP + w0 + 5));
                    double ang0 = GeTools.RadianToDegree(Math.Atan(w0 / gradeLaye));
                    Arc arc1 = new Arc(pt.C3D(wP-5, -R), R, GeTools.DegreeToRadian(ang0), GeTools.DegreeToRadian(90)) { LayerId = lt[layer] };

                    recorder.AppendEntity((Entity)arc1);
                    tr.AddNewlyCreatedDBObject(arc1, true);

                    Arc arc2 = new Arc(pt.C3D(wP + w0+5, R - gradeLaye), R, GeTools.DegreeToRadian(180 + ang0), GeTools.DegreeToRadian(-90)) { LayerId = lt[layer] };

                    recorder.AppendEntity((Entity)arc2);
                    tr.AddNewlyCreatedDBObject(arc2, true);
                    //Arc arc3 = new Arc(pt.C3D(w * 0.5 - 1.4 + w0, -1 * scale + gradeLaye), 1 * scale, GeTools.DegreeToRadian(90), GeTools.DegreeToRadian(180 - ang0)) { ColorIndex = 4 };



                    PLPloter.AddLine(db,arc2.StartPoint.C2D(), arc1.StartPoint.C2D(), ref ext, layer);
                    line2 = new Line( arc1.StartPoint.C3D(),arc2.StartPoint.C3D());
                    PLPloter.AddLine(db,arc2.EndPoint.C2D(), pt.C2D(wP + w0 + wPi, -gradeLaye), ref ext, layer);
                }

                tr.Commit();
            }



        }


        public static void CreateSampleS(Database db, ref Extents2d ext, Point2d pt, double R, double ND, double scale=1,string layer = "H钢筋")
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Circle C2 = new Circle();
                C2 = new Circle(pt.C3D(), Vector3d.ZAxis, R);
                C2.LayerId = lt[layer];
                double h1 = R / 3 * Math.Sin(GeTools.DegreeToRadian(45));
               
                Arc arc1 = new Arc(pt.C3D(h1*2, -h1 * 2), R / 3, GeTools.DegreeToRadian(135), GeTools.DegreeToRadian(315)) { LayerId = lt[layer] }; ;

                PLPloter.AddLine(db,pt.C2D(h1, -h1), pt.C2D(h1+ R / 4, -h1+R / 4), ref ext, "H钢筋");
                double length = Math.PI * ND * (4 + 2 * 0.5)*0.5 + 10 * ND;
                TextPloter.AddDBText(db,pt.C3D(0, -h1*2), ref ext, length.ToString("0.0"), scale);
                recorder.AppendEntity((Entity)arc1);
                tr.AddNewlyCreatedDBObject(arc1, true);
                recorder.AppendEntity((Entity)C2);
                tr.AddNewlyCreatedDBObject(C2, true);


                ext = ext.Add(new Extents2d(C2.Bounds.Value.MinPoint.C2D(), C2.Bounds.Value.MaxPoint.C2D()));


                tr.Commit();
            }

        }
    }

    public enum eLocation
    {
        Top,
        Middle,
        Bottom
    }


   
}
